var text = [{
  title: '结题申请',  mod_title: '修改项目', back: '返回', confirm: '确定',
  btn: '申请结题', sub_reject: '驳回\n申请', sub_accept: '同意\n结题',
  ph1: '一句话总结该项目', ph2: '简述项目概况', ph3: '对自我的评价', ph4: '请概述驳回理由', 
}, {
  title: '结题申请',  mod_title: '修改项目', 
  btn: '申请结题', sub_reject: '驳回\n申请', sub_accept: '同意\n结题',
  ph1: '一句话总结该项目', ph2: '简述项目概况', ph3: '对自我的评价', ph4: '请概述驳回理由', 
}], 
modal = [{
  loading: '提交中', fail: '申请内容未填写完整', succ: '申请已发送',
  accept: '同意结题', accept0: '同意结题后该项目将认定结束', accept1: '项目结题成功',
  reject: '申请已驳回', process: '处理中'
}, {
  loading: '提交中', fail: '申请内容未填写完整', succ: '申请已发送',
  accept: '同意结题', accept0: '同意结题后该项目将认定结束', accept1: '项目结题成功',
  reject: '申请已驳回', process: '处理中'
}],
abilities = [],
form = { content: {} },
animating = 0;

Page({
  data: {
    showMask: true,
    showText: false,
    hideGroup2: true,         // 显示另一组按钮
    REJECT: false,           // 驳回申请的输入状态
    btnClr: '#ffeec6',       // 按钮颜色
    textClr: '#aaa',         // 按钮文字颜色

  },

  onLoad(option) {
    form.idPracticeItem = option.id;
    if (option.content) {
      let item = JSON.parse(decodeURIComponent(option.content));
      console.log(item)
      this.setData({
        SUBMIT: item.content
      });
    }
    this.setData({
      text: text[wx.lang],
      today: wx.formatTime(new Date, '-'),
    });
  },

  onShow() {
    wx.onShow(this, text[wx.lang].title, wx.theme);
    let clr = wx.rgb(wx.theme)
    this.setData({ btnArea: clr.join(',')  })
    let obj = ['show1', 'show2', 'show3', 'show4', 'show5', 'show6'];
    wx._slide_v(this, obj, wx.system.height, 0, () => {
      wx.slide_v(this, obj, 0, 1, .5, () => {
        this.setData({ showText: 1 });
      });
    })
  },

  input(e) {
    form.content[e.currentTarget.id] = e.detail.value;
    this.lightUp();
  },

  lightUp() {
    // returns whether the button is available 
    if (!!form.content.title && !!form.content.taskSummary && !!form.content.selfSummary) {
      this.setData({ textClr: '#fff', btnClr: '#f4d66d' });
      return true;
    } else {
      this.setData({ textClr: '#aaa', btnClr: '#ffeec6' });
      return false;
    }
  },

  submit() {
    // avoid the duplicate submit
    if (!this.lightUp()) {
      wx.toast(modal[wx.lang].fail);
      return;
    } 
    wx.post('PracticeItem.uploadPractice', form, res => {
      wx.toast(modal[wx.lang].succ, 1);
      setTimeout(() => {
        wx.navigateBack({
          delta: 2,
        })
      }, 2000)
    }, modal[wx.lang].loading)
  },
  
  acceptSubmit() {
    wx.modal(modal[wx.lang].accept, modal[wx.lang].accept0, 0, null, null, null, () => {
      wx.post('PracticeItem.finishPractice', form, res => {
        wx.toast(modal[wx.lang].accept1, 1);
        wx.appData.user.finish++;
        wx.appData.user.ongoing--;
        setTimeout(() => {
          wx.reLaunch({
            url: '../../me/me',
          })
        }, 2000);
      }, modal[wx.lang].process)
    })
  },

  rejectSubmit() {
    form.reject = form.content.reject;
    wx.post('PracticeItem.rejectApplication', form, res => {
      wx.toast(modal[wx.lang].reject, 1);
      setTimeout(() => {
        wx.appData.refreshTab = 1;
        wx.navigateBack({
          delta: 2,
        })
      }, 2000)
    }, modal[wx.lang].process)
  },


  // 向右滑动
  slide_right() {
    if (animating++) return;
    wx.slide_v(this, ['group2'], wx.system.height, 0, .5, () => {
      this.setData({ hideGroup2: true });
      wx._slide_v(this, ['group1'], wx.system.height, 0, () => {
        wx.slide_v(this, ['group1'], 0, 1, 0, () => {
          animating = 0;
        });
      });
    });
    wx.slide_h(this, ['show4'], wx.system.width, 0, .5, () => {
      wx._slide_h(this, ['show1', 'show2', 'show3'], -1 * wx.system.width, 0, () => {
        this.setData({
          REJECT: false
        });
        wx.slide_h(this, ['show1', 'show2', 'show3'], 0, 1, 1)
      });
    });
  },

  // 向左滑动
  slide_left() {
    if (animating++) return;
    wx.slide_v(this, ['group1'], wx.system.height, 0, .5, () => {
      this.setData({ hideGroup2: false });
      wx._slide_v(this, ['group2'], wx.system.height, 0, () => {
        wx.slide_v(this, ['group2'], 0, 1, 0, () => {
          animating = 0;
        })
      })
    })
    wx.slide_h(this, ['show1', 'show2', 'show3'], -1 * wx.system.width, 0, .5, () => {
      wx._slide_h(this, ['show4'], wx.system.width, 0, () => {
        this.setData({
          REJECT:true
        });
        wx.slide_h(this, ['show4'], 0, 1, 1)
      });
    });
  },

  onShareAppMessage: function () {

  }


})